<!--头部公共部分-->
{include file="common/header" /}

<body class="layui-anim layui-anim-up">
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>设置</a>
        <a>
          <cite>权限管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;padding-top:20px;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
            <div class="layui-input-inline">
                <input type="text" name="title" placeholder="请输入用户组名" autocomplete="off" class="layui-input">
            </div>

            {if $type == 1}
            <div class="layui-input-inline">
                <select name="type_status" lay-verify="required" lay-search="">
                    <option value="">请选择用户组</option>
                    <option value="1">超级用户组</option>
                    <option value="2">系统用户组</option>
                    <option value="3">区域用户组</option>
                    <option value="4">校区用户组</option>
                    <option value="5">员工用户组</option>
                </select>
            </div>
            {/if}

            {if $type == 2}
            <div class="layui-input-inline">
                <select name="type_status" lay-verify="required" lay-search="">
                    <option value="">请选择用户组</option>
                    <option value="3">区域用户组</option>
                    <option value="4">校区用户组</option>
                    <option value="5">员工用户组</option>
                </select>
            </div>
            {/if}

            {if $type == 3}
            <div class="layui-input-inline">
                <select name="type_status" lay-verify="required" lay-search="">
                    <option value="">请选择用户组</option>
                    <option value="4">校区用户组</option>
                    <option value="5">员工用户组</option>
                </select>
            </div>
            {/if}

            {if $type == 4}
            <div class="layui-input-inline">
                <select name="type_status" lay-verify="required" lay-search="">
                    <option value="5" selected>员工用户组</option>
                </select>
            </div>
            {/if}

            <div class="layui-input-inline">
                <select name="status">
                    <option value="">请选状态</option>
                    <option value="1">启用中</option>
                    <option value="0">禁用中</option>
                </select>
            </div>
            <button class="layui-btn" data-type="reload" id="search"><i class="layui-icon">&#xe615;</i>筛选</button>
            <button class="layui-btn layui-btn-danger" id="clear"><i class="layui-icon">&#xe639;</i>重置</button>
            <a href="javascript:;" type="button" class="layui-btn layui-bg-blue" id="table-add"><i class="layui-icon">&#xe608;</i>新增用户组</a>
        </div>
    </div>
    <table class="layui-hide" id="LAY_table_user" lay-filter="table"></table>
    <script type="text/html" id="barTable">
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="rule">分配权限</a>
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>{{# if(d.status == 0){ }}
        <a class="layui-btn layui-btn-xs" style="background-color: #009688" lay-event="check">激活</a>{{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="check">禁用</a>{{# } }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    layui.use(['element', 'table', 'layer', 'laytpl'], function () {
        let $ = layui.$,
            element = layui.element,
            table = layui.table,
            layer = layui.layer,
            laytpl = layui.laytpl;
        let type = "{$type}";
        $('#table-add').click(function () {
            if(type == 1){
                layer.open({
                    type: 2,
                    skin: 'layui-layer-lan',
                    closeBtn: 2,
                    title: '选择集团',
                    area: ['50%', '60%'],
                    shadeClose: false,
                    content: "{:url('Auth/company')}",
                });
            }else{
                let company_id = "{$company_id}";
                let url = "{:url('Auth/auth_add')}?company_id="+company_id;
                layer.open({
                    type: 2,
                    title: '新增用户组',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['60%', '50%'],
                    content: url
                });
            }
        });
        //数据表格 方法级渲染
        let tableIns = table.render({
            elem: '#LAY_table_user',
            url: '{:url("Auth/auth")}',
            method: 'POST',
            cols: [[
                {field: 'id', title: 'ID', width: 100, sort: true, fixed: false, align: 'center'
                }, {field: 'title', title: '用户组名', width: 200, align: 'center'
                }, {field: 'company_name', title: '集团名称', width: 200, align: 'center'
                }, {field: 'type_text', title: '用户组类型', width: 200, align: 'center'
                }, {field: 'group_type_text', title: '用户组属性', width: 200, align: 'center'
                }, {field: 'status_text', title: '状态', width: 200, align: 'center'
                }, {fixed: 'right', title: '操作', width: 260, align: 'center', toolbar: '#barTable'
                }]],
            limit: 10,
            page: true,
            height: 'full-234',
            done: function (res, curr, count) {
                $("[data-field='id']").css('display','none');
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            }
        });
        //监听工具条
        table.on('tool(table)', function (obj) {
            let data = obj.data; //获得当前行数据
            let layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') { //删除
                layer.confirm('真的删除么', function (index) {
                    let isLoad;
                    $.post("{:url('Auth/auth_del')}", {id: data.id}, function (result) {
                        if (result.code == 0) {
                            layer.msg(result.msg, {time: 2000});
                            setTimeout(function () {
                                obj.del(); //删除对应行（tr）的DOM结构
                                location.reload();
                            }, 2000)
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 2000});
                        }
                    }, "json")
                });
            } else if (layEvent === 'rule') {
                //分配权限
                let url = '{:url("Auth/rule_group")}?id=' + data.id + '&type=1';
                layer.open({
                    type: 2,
                    title: '分配权限',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['90%', '90%'],
                    content: url
                });
            } else if (layEvent === 'edit') {
                //编辑
                //do something
                let url = '{:url("Auth/auth_edit")}?id=' + data.id + '&company_id=' + data.company_id;
                layer.open({
                    type: 2,
                    title: '编辑用户组',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['40%', '35%'],
                    content: url
                });
            } else if (layEvent === 'check') {
                let status;
                if (data.status == 0) {
                    status = 1;
                } else {
                    status = 0;
                }
                //激活与禁用状态改变
                $.post("{:url('Auth/auth_check_status')}", {
                    id: data.id,
                    status: status
                }, function (rJson) {
                    if (rJson.code == 0) {
                        layer.msg(rJson.msg, {
                            time: 2000
                        });
                        setTimeout(function () {
                            location.reload();
                        }, 2000)
                    } else {
                        layer.msg(rJson.msg, {
                            icon: 2,
                            time: 2000
                        });
                    }
                }, "json")
            }
        });
        //搜索项
        $('#search').click(function() {
            let field = {};
            field.status = $('select[name=status] option:selected').val();
            field.type_status = $('select[name=type_status] option:selected').val();
            field.title = $('input[name=title]').val().trim();
            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //清除
        $('#clear').click(function() {
            $('.layui-form-pane').find('.layui-input-inline').each(function(index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'status': '',
                    'type_status': '',
                    'title': ''
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
